package com.skynet.supervision.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.skynet.supervision.beans.response.SysDictDataVo;
import com.skynet.supervision.beans.response.SysDictTypeVo;
import com.skynet.supervision.common.PageResults;
import com.skynet.supervision.dao.SysDictDataMapper;
import com.skynet.supervision.dao.SysDictTypeMapper;
import com.skynet.supervision.entities.SysDictData;
import com.skynet.supervision.entities.SysDictType;
import com.skynet.supervision.services.SysDictService;
import com.skynet.supervision.utils.PageUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class SysDictServiceImpl implements SysDictService {
    private final SysDictTypeMapper sysDictTypeMapper;
    private final SysDictDataMapper sysDictDataMapper;


    public SysDictServiceImpl(SysDictTypeMapper sysDictTypeMapper, SysDictDataMapper sysDictDataMapper) {
        this.sysDictTypeMapper = sysDictTypeMapper;
        this.sysDictDataMapper = sysDictDataMapper;
    }

    @Override
    public void addDictType() {
        SysDictType dictType = new SysDictType();
        dictType.setName("name");
        dictType.setKey("key");
        dictType.setStatus(1);
        dictType.setSort(1);
        sysDictTypeMapper.insert(dictType);
    }

//    @Override
//    public PageResults<SysDictTypeVo> listPagedDictType(ListPagedDictTypeRequest request) {
//        Page<SysDictTypeVo> pageQ = PageUtils.checkAndBuildPage(request.getPage(), request.getPageSize());
//        return PageResults.of(sysDictTypeMapper.listPaged(pageQ, request.getKey()));
//    }

    @Override
    public List<SysDictDataVo> listDictValues(String key) {
        return sysDictDataMapper.find(Lists.newArrayList(key));
    }

//    @Override
//    public List<DictDataListItem> allDictValues() {
//        List<DictDataListItem> dictDataListItems = new ArrayList<>();
//
//        List<SysDictData> sysDictData = this.sysDictDataMapper.selectList(new QueryWrapper<>());
//        Map<String, List<SysDictData>> dictData = sysDictData.stream().collect(Collectors.groupingBy(res -> res.getKey()));
//        Set<String> set = dictData.keySet();
//        set.forEach(res->{
//            SysDictType dictType = this.sysDictTypeMapper.selectOne(new QueryWrapper<SysDictType>().eq("key", res));
//            DictDataListItem dictDataListItem = new DictDataListItem();
//            List<SysDictDataVo> sysDictTypeVoList = new ArrayList<>();
//            dictData.get(res).forEach(one->{
//                SysDictDataVo sysDictDataVo = new SysDictDataVo();
//                BeanUtils.copyProperties(one,sysDictDataVo);
//                sysDictTypeVoList.add(sysDictDataVo);
//            });
//            dictDataListItem.setKey(res);
//            dictDataListItem.setName(dictType.getName());
//            dictDataListItem.setDictList(sysDictTypeVoList);
//            dictDataListItems.add(dictDataListItem);
//        });
//        return dictDataListItems;
//    }
}
